package com.hdaccp.cloud.controller;

import com.hdaccp.cloud.bean.Users;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

@RestController
public class UsersConsumerController {

    @Autowired
    private RestTemplate restTemplate ;
    @HystrixCommand(fallbackMethod = "getDefaultUser")
    @RequestMapping("/getConsumerUsers/{id}")
    public Users getUsers(@PathVariable("id") int id){
       Users users = restTemplate.getForObject("http://T171CLOUD/getUsers/" + id,Users.class) ;
        return users ;
    }

    @HystrixCommand(fallbackMethod = "queryDefaultUser")
    @RequestMapping("/queryConsumerUsers")
    public List<Users> query(){
        List<Users> list = restTemplate.getForObject("http://T171CLOUD/query/",List.class) ;
        return list ;
    }

    public List<Users> queryDefaultUser(){
        return new ArrayList<>() ;
    }

    public Users getDefaultUser(int id){
        Users users = new Users() ;
        users.setId(0);
        users.setName("默认用户");
        users.setAddress("翱翔集团");
        return users ;
    }

}
